package com.swt.draw.controller.gztTool;

import com.swt.draw.entity.Admin;
import com.swt.draw.entity.Fla55;
import com.swt.draw.example.GztToolExample;
import com.swt.draw.service.Fla55Service;
import com.swt.draw.service.GztToolService;
import com.swt.draw.vo.GztToolVO;
import com.swt.draw.vo.TreetableResponseVO;
import com.swt.draw.vo.page.PageVO;
import com.swt.draw.vo.tableDataResp.TableDataRespVO;
import com.swt.draw.vo.treetable.Fla55TreeVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/10/19 0019 21:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("/tool")
public class GztToolController {

    @Resource
    private GztToolService gztToolService;
    @Resource
    private Fla55Service fla55Service;

    /**
     * @return com.swt.draw.vo.tableDataResp.TableDataRespVO
     * @Author ZPF
     * @Description 查询工具表
     * @Date 21:49 2019/10/19 0019
     * @Param []
     **/
    @GetMapping("/list")
    public TableDataRespVO getGztToolList(Integer page, Integer limit) {
        TableDataRespVO resp = new TableDataRespVO();
        PageVO pageVO = new PageVO();
        pageVO.setStart((page - 1) * limit);
        pageVO.setEnd(page * limit);
        List<GztToolVO> toolVOS = gztToolService.getToolList(null, null, pageVO);
        Long total = gztToolService.getToolCount(null);
        resp.setCount(total);
        resp.setCode(0);
        resp.setMsg("success");
        resp.setData(toolVOS);
        return resp;
    }

    /**
     * @return com.swt.draw.vo.tableDataResp.TableDataRespVO
     * @Author ZPF
     * @Description 获取原件分类
     * @Date 23:28 2019/10/23 0023
     * @Param [page, limit]
     **/
    @GetMapping("/fenlei")
    public TableDataRespVO getGztToolFenlei(Integer page, Integer limit) {
        TableDataRespVO resp = new TableDataRespVO();
        PageVO pageVO = null;
        if (page != null && limit != null && page > 0 && limit > 0) {
            pageVO = new PageVO();
            pageVO.setStart((page - 1) * limit);
            pageVO.setEnd(page * limit);
        }
        List<Fla55> fla55s = fla55Service.getFla55List(null, pageVO);
        Integer total = fla55Service.getFla55Count();
        resp.setCount(total);
        resp.setCode(0);
        resp.setMsg("success");
        resp.setData(fla55s);
        return resp;
    }

    /**
     * @return java.util.List<com.swt.draw.vo.treetable.Fla55TreeVo>
     * @Author ZPF
     * @Description 获取分类树形结构
     * @Date 20:15 2019/10/24 0024
     * @Param []
     **/
    @GetMapping("/flTree")
    public TreetableResponseVO<Fla55TreeVo> getFla55TreeData() {
        TreetableResponseVO<Fla55TreeVo> responseVO = new TreetableResponseVO();
        List<Fla55> list = fla55Service.getAll();
        List<Fla55TreeVo> result = new ArrayList<>();
        // 格式化，满足树状组件要求
        short lbid = -1;
        for (Fla55 fla55 : list) {
            if (fla55.getLbid() != lbid) {
                // 添加父类
                Fla55TreeVo parent = new Fla55TreeVo();
                parent.setParentId((short) 0);
                parent.setLbid(fla55.getLbid());
                parent.setLb(fla55.getLb());
                parent.setTableId(fla55.getId());
                result.add(parent);
            }
            // 添加子类
            Fla55TreeVo child = new Fla55TreeVo();
            child.setParentId(fla55.getLbid());
            child.setMcid(fla55.getMcid());
            child.setLb("");
            child.setDmmc(fla55.getDmmc());
            child.setLbid((short) -1);
            child.setTableId(fla55.getId());
            result.add(child);
            lbid = fla55.getLbid();
        }
        responseVO.setCount((long) result.size());
        responseVO.setCode(0);
        responseVO.setData(result);
        responseVO.setMsg("ok");
        return responseVO;
    }

}
